package com.yystudy.system.controller;


import com.yystudy.common.result.Result;
import com.yystudy.model.system.SysDept;
import com.yystudy.system.annotation.Log;
import com.yystudy.system.enums.BusinessType;
import com.yystudy.system.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 组织机构 前端控制器
 * </p>
 *
 * @author yystudy
 * @since 2023-10-17
 */
@RestController
@RequestMapping("/admin/system/sysDept")
@Api(tags = "部门管理接口")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;


    /**
     * 返回部门列表数据
     * @return
     */
    @ApiOperation(value = "部门列表")
    @GetMapping("/findNodes")
    @PreAuthorize("hasAuthority('bnt.sysDept.list')")
    public Result findNodes() {
        List<SysDept> list = sysDeptService.findNodes();
        return Result.ok(list);
    }


    /**
     * 添加部门
     * @param sysDept
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysDept.add')")
    @ApiOperation(value = "添加部门")
    @PostMapping("/save")
    @Log(title = "部门管理",businessType = BusinessType.INSERT)
    public Result save(@RequestBody SysDept sysDept){
        sysDeptService.save(sysDept);
        return Result.ok();
    }


    /**
     * 根据id进行查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id进行查询")
    @GetMapping("/findNode/{id}")
    @PreAuthorize("hasAuthority('bnt.sysDept.list')")
    public Result findNode(@PathVariable Long id) {
        SysDept byId = sysDeptService.getById(id);
        return Result.ok(byId);
    }


    /**
     * 修改更新数据
     * @param sysDept
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysDept.update')")
    @Log(title = "部门管理",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改更新数据")
    @PutMapping("/update")
    public Result update(@RequestBody SysDept sysDept){
        sysDeptService.updateById(sysDept);
        return Result.ok();
    }


    /**
     * 根据id删除部门
     * @param id
     * @return
     */
    @Log(title = "部门管理",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysDept.remove')")
    @ApiOperation(value = "根据id删除部门")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        sysDeptService.removeById(id);
        return Result.ok();
    }


}

